<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title id="user_manager">用户管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../static/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../static/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <!--搜索条件区域-->
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" id="search-form">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="uAccount" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline" pane>
                            <label class="layui-form-label">性别</label>
                            <div class="layui-input-block">
                                <input type="radio" name="uGender" value="1" title="男">
                                <input type="radio" name="uGender" value="0" title="女" >
                                <input type="radio" name="uGender" value="" title="无选择" >
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            <button  class="layui-btn layui-btn-warm"  lay-filter="data-reset-btn"><i class="layui-icon">&#xe666;</i> 重 置</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <!--表格区域-->
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        <!--行工具栏-->
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit"><i class="layui-icon">&#xe642;</i>编辑</a>
            <a class="layui-btn layui-btn-warm layui-btn-xs data-count-reset" lay-event="reset"><i class="layui-icon">&#xe669;</i> 重置密码</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete"><i class="layui-icon">&#xe640;</i>删除</a>
        </script>
        <!--添加修改窗口弹出层-->
        <div style="display: none;padding:5px" id="addOrUpdateWindow">
            <form class="layui-form layuimini-form " id="dataFrm" lay-filter="dataFrm">
                <!--主键隐藏域，保存主键-->
                <input type="hidden" name="uId" />
                <div class="layui-form-item">
                    <label class="layui-form-label required">用户名</label>
                    <div class="layui-input-block">
                        <input type="text" name="uAccount" lay-verify="required" lay-reqtext="用户名不能为空" placeholder="请输入用户名" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">昵称</label>
                    <div class="layui-input-block">
                        <input type="text" name="uName"  placeholder="请输入昵称" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label ">邮箱</label>
                    <div class="layui-input-block">
                        <input type="text" name="uMail" placeholder="请输入邮箱" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">性别</label>
                    <div class="layui-input-block">
                        <input type="radio" name="uGender" value="1" title="男" checked>
                        <input type="radio" name="uGender" value="0" title="女" >
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script src="../../static/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table','layer','upload','laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layer = layui.layer;

        var tableIns = table.render({
            elem: '#currentTableId',
            url: '/user/list',
            toolbar: '#toolbarDemo',
            cols: [
                [
                {field: 'uId', width: 100, title: 'ID', sort: true},
                {field: 'uAccount', width: 140, title: '用户名'},
                {field: 'uName', width: 135, title: '昵称', sort: true},
                {field: 'uMail', title: '邮箱', minWidth: 150},
                {field: 'uGender', width: 80, title: '性别', sort: true,align:"center",templet:function (d) {
                            return d.uGender === 1 ? "男" : "女";
                    }},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            done: function(res,curr,count){ //解决当一页的最后一个数据被删除后，不会跳转到前一页的问题
                //判断当前页码是否大于1且当前页数据量为0
                if(curr > 1 && res.data.length == 0){
                    var pageValue = curr - 1;
                    //刷新数据表格的数据
                    tableIns.reload({
                        page: {curr:pageValue}
                    })
                }
            }
        });

        // 监听模糊查询操作
        form.on('submit(data-search-btn)', function (data) {

            //执行搜索重载
            tableIns.reload({
                page: {
                    curr: 1
                }
                , where: data.field
            });

            return false;
        });
        //监听重置操作
        form.on('click(data-reset-btn)',function () {
                $("#search-form").reset();
        });

        //保存提交地址和窗口的索引
        var url,mainIndex;
        /**
         * 打开修改窗口
         */
        function openUpdateWindow(data){
            mainIndex = layer.open({
                title: '修改用户',
                type: 1,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['40%', '80%'],   //宽，高
                content: $("#addOrUpdateWindow"),   //引用的内容窗口
                success: function () {
                    //提交地址
                    url = "/user/update";
                    //表单数据回显
                    form.val("dataFrm",data);
                }
            })
        }
        //监听表单提交事件
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            //发送请求
            $.post(url,data.field,function (result) {
                if (result.success){
                    //成功
                    layer.alert(result.message,{icon:1});
                    //刷新当前数据表格
                    tableIns.reload();
                    //关闭当前窗口
                    layer.close(mainIndex);
                }else {
                    //失败
                    layer.alert(result.message,{icon:2});
                }
            },"json");
            return false;
        });

        //监听行工具栏事件
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                openUpdateWindow(data);
                return false;
            } else if(obj.event==='reset'){
                resetPwd(data);
            } else if (obj.event === 'delete') {
                deleteById(data);
            }
        });
        function resetPwd(data) {

            $.post("/user/reset",{"uId":data.uId},function (result) {
                if (result.success){
                    //成功
                    layer.alert(result.message,{icon:1});
                    //刷新当前数据表格
                    tableIns.reload();
                }else{

                    //提示
                    layer.alert(result.message,{icon:2})
                }
            },"json");
        }
        //删除用户方法
        function deleteById(data) {
            //提示用户是否确认删除
            layer.confirm('确定要删除用户[<span style="color:red">'+data.uAccount+'</span>]吗？',{icon:3,title:"提示"}, function (index) {
                // obj.del();
                //发送删除请求
                $.post("/user/del", {"uId":data.uId},function (result) {
                    if (result.success){

                        //成功
                        layer.alert(result.message,{icon:1});
                        //刷新当前数据表格
                        tableIns.reload();
                    }else{
                        //提示
                        layer.alert(result.message,{icon:2})
                    }
                },"json");
                layer.close(index);
            });
        }
    });
</script>

</body>
</html>